﻿
var siftJson = { action: "siftJson", parameter: {}, shiftSum: 0 }
//上次查询的条件
var oldSiftAjaxParameter = null;
var nowPageIndex = 0;
//getMoreType:keyword,sift
var getMoreType = "keyword";
//分页数
var pageSize = 10;

    var siftAjaxStr = { action: "siftJson", parameter: ""}
    var siftMessage = {
            wait:"筛选中...",
            error:"服务错误"
    }
    function t1(evt) {

        evt = $.event.fix(evt);
        var el = $(evt.target);
        var _current = null;
        if (el.hasClass("subItem")) {
            _current = el;
        } else {
            _current = el.parent('.subItem')
        }

        if (_current == null) return;

        ////////////////////

        var _parent = _current.parent(".field");
        var _input = _current.children("input:first-child");
        var _fieldName = _input.attr("name");
        if (el.get(0) != _input.get(0)) { _input.attr("checked", !_input.attr("checked")) }


        var _inputs = _parent.find("input:checked");
        var _value = new Array();

        _inputs.each(function () {
            _value[_value.length] = this.value;
        })
        
        if (_value.length != 0) {
            siftJson.parameter[_fieldName] = _value.join("|");
        } else {
            siftJson.parameter[_fieldName] = null;
        }
        //如果都为空(无筛选)
        

        siftAjaxStr.parameter = siftToArrStr(siftJson.parameter);


        

        $("#siftSumVal").html(siftMessage.wait)

        //统计数的提交
        $.get("/shop/reSum", siftAjaxStr, function (str) {
            $("#siftSumVal").html(str);
            $("#navPageSumVal").html(str);
            siftJson.shiftSum=str;
        });
    }
    //_str = "{i:[1,2,3,4,5],h:[1,2,3,4]}"
    function siftToArrStr(json) {
        var _str = "";
        var _i = 0;
        for (var i in json) {
            if (json[i] == "" || json[i]==undefined) {continue;}
            if (_i != 0) { _str += "," }
            _str += i + ":" + json[i] + "";
            _i++;           
        }
        return _str + "";
    }
    //最到更多（下一页)
    function nextViewSubmit() {
        nowPageIndex++;

        if (getMoreType == "keyword") {
            shopSearch()
        } else if (getMoreType == "sift") {
            listViewSubmit();
        }

        
        
    }
    //first:true 重新查询
    function listViewSubmit(first) {

        if(first==true){
            nowPageIndex = 0;
            oldSiftAjaxParameter = siftAjaxStr.parameter;
        }

        //self.location.href = "/shop/shopSift?parameter=" + encodeURI(siftAjaxStr.parameter);
        //$("#shopListPanle").load("/shop/shopSift", { parameter: siftAjaxStr.parameter, resultType: "ajaxHTML", pageIndex: siftJson.pageInfo.pageIndex, pageSize: siftJson.pageInfo.pageSize });

        $.post("/shop/shopSift", { parameter: oldSiftAjaxParameter, resultType: "ajaxHTML", pageIndex: nowPageIndex, pageSize: pageSize }, function (json) {

            if (first == true) {
                $("#shopListPanle").html("");
            }
            //判断还有无更多数据
            if (json.indexOf("<more>true</more>") >= 0) {
                json = json.replace("<more>true</more>", "")
                $("#nextViewSubmitBtn").show();
            } else {
                $("#nextViewSubmitBtn").hide();
            }

            $("#shopListPanle").append(json);
            $("navPageNowVal").html("0 - " + (nowPageIndex + 1) * pageSize);
            getMoreType = "sift";




        });

    }
    function shopSearch(first) {
        var resultType="";
        if (first == true) {
            self.location.href = "/shop/search?keyword=" + document.getElementById("keyword").value +"&pageIndex=0&pageSize=" + pageSize;

        } else {
            resultType = "ajaxHTML";
            oldSiftAjaxParameter = document.getElementById("keyword").value;
            $.post("/shop/search", { keyword: oldSiftAjaxParameter, resultType: resultType, pageIndex: nowPageIndex, pageSize: pageSize }, function (json) {

                if (first == true) {
                    $("#shopListPanle").html("");
                }
                //判断还有无更多数据
                if (json.indexOf("<more>true</more>") >= 0) {
                    json = json.replace("<more>true</more>", "");
                    $("#nextViewSubmitBtn").show();
                } else {
                    $("#nextViewSubmitBtn").hide();
                }

                $("#shopListPanle").append(json);
                $("navPageNowVal").html("0 - " + (nowPageIndex + 1) * pageSize);
                getMoreType = "keyword";

            })

        }


    }

    //parameter 回写
    function reRenderSift() {
        var URLParams, URLKeyword;
        if (document.location.search.substr(1) == "") return;

        //取得url传过来的name参数


        var URLStr = decodeURI(document.location.search.substr(1));
        var URLArguments = URLStr.split("&");
        for (var _s in URLArguments) {
            if (URLArguments[_s].indexOf("parameter=") >= 0) {
                URLParams = URLArguments[_s].replace("parameter=", "").split(",");

                continue;
            }
            if (URLArguments[_s].indexOf("keyword=") >= 0) {
                URLKeyword = URLArguments[_s].replace("keyword=", "")
                continue;
            }
        }

        //回写keyword
        if (URLKeyword && URLKeyword.trim() != "") {
            document.getElementById("keyword").value = URLKeyword;
        }
        //回写URLParams
        //特别处理ALL
        if (URLParams == undefined || (URLParams[0] && URLParams[0] == "all" || URLParams[0].trim() == "")) { return }

        for (var item = 0; item < URLParams.length; item++) {


            //取得参数组
            var vals = URLParams[item].split(":");
            //取得字段对应值的String
            //var _vsStr = vals[1].substring(1, vals[1].length - 1);
            var _vsStr = vals[1];
            //复写siftJson
            siftJson.parameter[vals[0]] = _vsStr
            //转换字段对应值为array
            var _vs = _vsStr.split("|")
            var nameEls = document.getElementsByName(vals[0]);

            for (var i = 0; i < nameEls.length; i++) {

                for (var vali = 0; vali < _vs.length; vali++) {
                    if (nameEls[i].value == _vs[vali]) {

                        nameEls[i].checked = true;
                    }
                }
            }


        }
    }
    $(function(){reRenderSift();});